package com.ljs.spring_fastdfs.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;

@Configuration
public class DruidConfig {
    /**
     *   * 注入数据源
     *   * @return
     *   
     */
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    /**
     *   * 配置监控
     *   * @return
     *   
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new
                StatViewServlet(), "/druid/*");
        HashMap<String, String> map = new HashMap<>(2);
        map.put("loginUsername", "ljs");
        map.put("loginPassword", "123456");/*要注册，登录http://localhost:8082/druid/sql.html*/
        bean.setInitParameters(map);
        return bean;
    }

    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
       /* B站 IT楠老师
        搞定
        4、Thymeleaf模板引擎
                Thymeleaf整合SpringBoot
        在pom.xml文件引入thymeleaf
        在application.properties（application.yml）文件中配置thymeleaf
        新建编辑控制层代码HelloController，在request添加了name属性，返回到前端hello.html再
        使用thymeleaf取值显示。*/
        bean.setFilter(new WebStatFilter());
        HashMap<String, String> map = new HashMap<>(8);
        map.put("exclusions", "*.js");
        bean.setInitParameters(map);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
